<?php
/**
 * Created by PhpStorm.
 * User: liaozidi
 * Date: 2019/1/4
 * Time: 22:20
 */
namespace app\shop\admin;
use app\admin\controller\Admin;
use app\common\builder\ZBuilder;

class GoodsType  extends Admin
{
    public function index()
    {
        $list = db('goods_type')->paginate(10);
        $page = $list->render();

        return ZBuilder::make('table')
            ->setPageTitle('商品模型列表')
            ->hideCheckbox()
            ->addColumns([
                ['id', 'ID'],
                ['name','模型名称'],
                ['right_button','操作']
            ])
            ->addTopButton('add')
            ->addRightButton('goodsAttrList',['title' => '属性列表', 'icon' => 'fa fa-fw fa-indent', 'href' => url('goodsAttrList',['id' =>'__id__'])])
            ->addRightButton('goodsSpecList',['title' => '规格列表', 'icon' => 'fa fa-fw fa-list', 'href' => url('goodsSpecList',['id' =>'__id__'])])
            ->addRightButtons('edit,delete')
            ->setPages($page)
            ->setRowList($list)
            ->fetch();
    }

    /**
     * 添加商品模型
     */
    public function add($id = 0)
    {
        if(request()->isPost()){
            $data = input('post.');
            $this->store($data,$id);
        }

        return ZBuilder::make('form')
            ->setPageTitle('添加商品模型')
            ->setUrl(url('add'))
            ->isAjax(false)
            ->addText('name','商品模型名称','')
            ->fetch();
    }

    public function edit($id = 0)
    {
        if(request()->isPost()){
            $data = input('post.');
            $this->store($data,$id);
        }
        $data = db('goods_type')->where('id',$id)->find();

        return ZBuilder::make('form')
            ->setPageTitle('编辑商品模型')
            ->setUrl(url('edit'))
            ->isAjax(false)
            ->addText('name','商品模型名称','')
            ->setFormData($data)
            ->fetch();
    }

    public function delete($ids = 0)
    {
        $res = db('goods_type')->where('id',$ids)->delete();
        if($res){
            $this->success('删除成功','index');
        }else{
            $this->error('删除失败');
        }
    }


    private function store($data, $id = 0)
    {
        if($id == 0){
            $res = db('goods_type')->insert($data);
        }else{
            $res = db('goods_type')->where('id',$id)->update($data);
        }

        if($res){
            $this->success('保存成功','index');
        }else{
            $this->error('保存失败，请重试');
        }
    }
}
